<?php

class Topic extends CI_Controller {

	function __construct(){
		parent::__construct();
		
		$this->load->model('topic_model');
		$this->load->model('production_model');
	}

	function show(){

		$data['success'] = $this->session->flashdata('success');
		$data['error'] = $this->session->flashdata('error');

		$tid = (int)$this->uri->segment(3,0);
		if(!$tid)show_404();
		$t = $data['topic'] = $this->topic_model->get_topic_byid($tid);
		if(!$data['topic'])show_404();

		$p = $this->production_model->get_production_byid($t->pid);
		if(!$p) show_error('该商品可能已经被删除');

		$data['p'] = $p;
		$this->load->library('form_validation');
		$this->form_validation->set_rules('content', '话题内容', 'trim|required|xss_clean');
		if($this->form_validation->run()){
			auth('/topic/show/'.$t->pid);
			$r = $this->topic_model->add_reply($tid);
			if($r['status']){
				$this->session->set_flashdata('success','回复成功');
				$this->db->query('UPDATE w_topic SET `reply_num` = `reply_num`+1 WHERE `id` = ' . $tid);

			}
			else{
				$this->session->set_flashdata('error',$r['msg']);
			}
			redirect(current_url(),'reflash');
		}
		

		$start = $this->uri->segment(4,0);
		$data['c'] = $this->topic_model->get_topic_reply($tid,$start,20);
		$data['num'] = $this->topic_model->get_topic_reply_num($tid);

		//pagenation
		$this->load->library('pagination');
		$config['base_url'] = '/topic/show/' . $tid . '/';
		$config['total_rows'] = $data['num'];
		$config['per_page'] = 20; 
		$config['full_tag_open'] = '<p class="page">';
		$config['full_tag_close'] = '</p>';
		$config['uri_segment'] = 4;
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['cur_tag_open'] = '<b>';
		$config['cur_tag_close'] = '</b>';
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$this->pagination->initialize($config);
		$data['start'] = $start;
		$data['page'] = $this->pagination->create_links();

		$this->db->query('UPDATE w_topic SET `view_num` = `view_num`+1 WHERE `id` = ' . $tid);

		$this->load->view('topic.tpl',$data);
	}


	function more(){
		$pid = (int)$this->uri->segment(3,0);
		if(!$pid)show_404();

		$p = $this->production_model->get_production_byid($pid);
		if(!$p) show_error('该商品可能已经被删除');

		$data['p'] = $p;
		
		
		$start = $this->uri->segment(4,0);
		$data['topic'] = $this->topic_model->get_topic($pid,$start,20);
		$data['topic_num'] =  $this->topic_model->get_topic_num($pid);


		//pagenation
		$this->load->library('pagination');
		$config['base_url'] = '/topic/more/' . $pid . '/';
		$config['total_rows'] = $data['topic_num'];
		$config['per_page'] = 20; 
		$config['full_tag_open'] = '<p class="page">';
		$config['full_tag_close'] = '</p>';
		$config['uri_segment'] = 4;
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['cur_tag_open'] = '<b>';
		$config['cur_tag_close'] = '</b>';
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();

		$this->load->view('topic/more.tpl',$data);
	}
}